﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;

using DragDraw.Util;

namespace DragDraw
{
    public partial class App : Application
    {
        public App()
        {
            this.Startup += this.Application_Startup;
            this.Exit += this.Application_Exit;
            this.UnhandledException += this.Application_UnhandledException;

            InitializeComponent();
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            // Load the main control
            WorldControl world = new WorldControl();
            OperationManager.Instance
                .CurrentSpaceManager.SetWidgetManager(world);
            OperationManager.Instance
                .CurrentSpaceManager.SetTaskBar(world.GetToolPanel());
            OperationManager.Instance.Init();
            this.RootVisual = world;
        }

        private void Application_Exit(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(OperationManager.Instance.MyUserInfo.UserID))
            {
                OperationManager.Instance.Logout();
            }
        }
        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            int i = 0;
            Exception err = e.ExceptionObject;
            string msg = "Unhandled Exception :\n";
            while (err != null)
            {
                msg += err.GetType().ToString() + " : " + err.Message + "\n";
                err = err.InnerException;
                i++;
            }
            HtmlPage.Window.Alert(msg);
        }
    }
}
